﻿using System;
using System.Web.UI;
using ReadyApps.Common;
using ReadyApps.Data;
using ReadyApps.Wiki.Templates;

namespace ReadyApps.Wiki.Views
{
    class WikiContentView : WikiContentViewBase
    {
        #region Variables and Constants

        private IWikiSecurityManager securityManager;
        private WikiControlSettings controlSettings;
        private bool pageIsVersioned;

        #endregion

        #region Events

        private event EventHandler editButtonClick;
        public event EventHandler EditButtonClick
        {
            add { editButtonClick += value; }
            remove { editButtonClick -= value; }
        }

        private event EventHandler editPermissionsButtonClick;
        public event EventHandler EditPermissionsButtonClick
        {
            add { editPermissionsButtonClick += value; }
            remove { editPermissionsButtonClick -= value; }
        }

        private event EventHandler viewRevisionsButtonClick;
        public event EventHandler ViewRevisionsButtonClick
        {
            add { viewRevisionsButtonClick += value; }
            remove { viewRevisionsButtonClick -= value; }
        }

        #endregion

        #region Constructor

        public WikiContentView(int pageID, bool wikiPageIsVersioned, IWikiDataProvider wikiDataProvider,
            IWikiUrlManager wikiUrlManager, IWikiSecurityManager wikiSecurityManager, WikiControlSettings wikiControlSettings)
            : base(pageID, wikiDataProvider, wikiUrlManager)
        {
            if (wikiSecurityManager == null)
            {
                throw new ArgumentNullException("wikiSecurityManager");
            }
            if (wikiControlSettings == null)
            {
                throw new ArgumentNullException("wikiControlSettings");
            }
            securityManager = wikiSecurityManager;
            controlSettings = wikiControlSettings;
            pageIsVersioned = wikiPageIsVersioned;
        }

        #endregion

        #region Public Methods

        public override void BuildControlTree(ControlCollection controls, bool raiseEvents)
        {
            Control container = new Control();

            // Instantiate the template
            ITemplate layoutTemplate = controlSettings.LayoutTemplate ?? new WikiDefaultLayoutTemplate();
            layoutTemplate.InstantiateIn(container);

            // Locate the content and wiki controls control
            Control contentControl = WebUtilities.FindControlRecursive(container, WikiController.CONTENT_CONTROL_ID);
            if (contentControl == null)
            {
                throw new Exception(string.Format("A server control with an ID of '{0}' was not found in the display template.", WikiController.CONTENT_CONTROL_ID));
            }
            Control controlsControl = WebUtilities.FindControlRecursive(container, WikiController.CONTROLS_CONTROL_ID);
            if (controlsControl == null)
            {
                throw new Exception(string.Format("A server control with an ID of '{0}' was not found in the display template.", WikiController.CONTROLS_CONTROL_ID));
            }

            // Fill the page content
            GetContentControls(contentControl);

            // Display the wiki controls that the user has access to
            if (securityManager.CanEditPermissions)
            {
                WikiControlButton editPermissionsButton = WikiControlButton.GetFromTemplate(controlSettings.ButtonTemplate);
                editPermissionsButton.ButtonControl.Text = "Permissions";
                editPermissionsButton.ButtonControl.Click += editPermissionsButtonClick;
                controlsControl.Controls.Add(editPermissionsButton.RootControl);
            }
            if (pageIsVersioned && securityManager.CanManageRevisions)
            {
                WikiControlButton manageRevisions = WikiControlButton.GetFromTemplate(controlSettings.ButtonTemplate);
                manageRevisions.ButtonControl.Text = "Revisions";
                manageRevisions.ButtonControl.Click += viewRevisionsButtonClick;
                controlsControl.Controls.Add(manageRevisions.RootControl);
            }
            if (securityManager.CanEditPage)
            {
                WikiControlButton editPageButton = WikiControlButton.GetFromTemplate(controlSettings.ButtonTemplate);
                editPageButton.ButtonControl.Text = "Edit";
                editPageButton.ButtonControl.Click += editButtonClick;
                controlsControl.Controls.Add(editPageButton.RootControl);
            }

            controls.Clear();
            controls.Add(container);
        }

        #endregion
    }
}
